import { View } from "../../../../mvc/core/View";
import { GButton } from "../../../../../com/mui/controls/GButton";
import { GTabBar } from "../../../../../com/mui/controls/GTabBar";
import { FilterConst } from "../../../../component/gconst/FilterConst";
import { MuiEvent } from "../../../../../com/mui/events/MuiEvent";
import { ButtonMini } from "./ButtonMini";
import { Language } from "../../../../../extend/language/Language";
import { OpenNameBtn } from "./OpenNameBtn";
import { GlobalClass } from "../../../../../com/mui/core/GlobalClass";
import { ShowBtn } from "./ShowBtn";
import { TaskTrackAccept } from "./TaskTrackAccept";
import { TaskTrackCurrent } from "./TaskTrackCurrent";
import { HTMLUtil } from "../../../../../com/gengine/utils/HTMLUtil";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { TaskInfo } from "../../../model/TaskInfo";
import { Cache } from "../../../cache/Cache";
import { ETaskGroup } from "../../../../../Message/Game/ETaskGroup";
import { Global } from "../../../../../com/gengine/global/Global";
import { TaskCache } from "../../../cache/TaskCache";
import { SceneRange } from "../../../scene/map/SceneRange";
import { TweenMax } from "../../../../../com/greensock/TweenMax";
import { Quint } from "../../../../../com/greensock/easing/Quint";
import { GameManager } from "../../../manager/GameManager";
import { ModuleType } from "../../common/ModuleType";
import { TaskTrack } from "./TaskTrack";
type int = number;
//class TaskTrackPanel
    
    export  class TaskTrackPanel extends View
    {
        constructor()
        {
            super();
            this._dragRect = new Object /* flash.geom.Rectangle */();
            mouseEnabled = false;
            this._bodySprite = new Object /* flash.display.Sprite */();
            addChild(this._bodySprite);
            this._bodySprite.mouseEnabled = false;
            this._bgSprite = new Object /* flash.display.Sprite */();
            this._bodySprite.addChild(this._bgSprite);
            this._bgSprite.mouseEnabled = false;
            this._bgSprite.mouseChildren = false;
            this._moveBtn = new GButton();
            this._moveBtn.width = 24;
            this._moveBtn.height = 22;
            this._moveBtn.label = "";
            this._moveBtn.styleName = "MoveBtn";
            this._bodySprite.addChild(this._moveBtn);
            this._tabBar = new GTabBar();
            this._tabBar.x = 53;
            this._tabBar.buttonWidth = 60;
            this._tabBar.buttonHeight = 22;
            this._tabBar.horizontalGap = -2;
            this._tabBar.buttonFilters = [FilterConst.glowFilter];
            this._tabBar.dataProvider = this.taskTabData;
            this._bodySprite.addChild(this._tabBar);
            this._tabBar.addEventListener(MuiEvent.GTABBAR_SELECTED_CHANGE, this.onTabBarChangeHandler);
            this._tabBar.buttonStyleName = "ButtonMini";
            this._cycleBtn = new ButtonMini();
            this._cycleBtn.x = 22;
            this._cycleBtn.setSize(32, 22);
            this._cycleBtn.label = Language.getString(20712);
            this._bodySprite.addChild(this._cycleBtn);
            this._cycleBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onCycleBtnClickHandler);
            this._openBtn = new OpenNameBtn();
            this._openBtn.x = 167;
            this._openBtn.y = 0;
            this._bodySprite.addChild(this._openBtn);
            this._openBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onOpenNameBtnClickHandler);
            this._shrinkBtn = new GButton();
            this._shrinkBtn.x = 187;
            this._shrinkBtn.y = -2;
            this._shrinkBtn.width = 26;
            this._shrinkBtn.height = 26;
            this._shrinkBtn.label = "";
            this._bodySprite.addChild(this._shrinkBtn);
            this._shrinkBtn.styleName = "ShrinkButton";
            this._shrinkBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onShrinkBtnClickHandler);
            this._bgImg = GlobalClass.getBitmap("TaskTrackBg");
            this._bgImg.y = 20;
            this._bgImg.width = this.width;
            this._bgImg.height = 145;
            this._bgSprite.addChild(this._bgImg);
            this._upLine = GlobalClass.getBitmap("Line");
            this._upLine.y = 20;
            this._upLine.x = -20;
            this._upLine.width = 300;
            this._bgSprite.addChild(this._upLine);
            this._bottomLine = GlobalClass.getBitmap("Line");
            this._bottomLine.y = 164;
            this._bottomLine.x = -20;
            this._bottomLine.width = 300;
            this._bgSprite.alpha = 1.6;
            this._bgSprite.addChild(this._bottomLine);
            this._hideBtn = new ShowBtn();
            this._hideBtn.x = 221;
            this._hideBtn.y = 11;
            addChild(this._hideBtn);
            this._moveBtn.addEventListener(flash.events.MouseEvent.MOUSE_DOWN, this.onMoveBtnMouseHandler);
            this._hideBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onHideBtnClickHandler);
            this._taskAccept = new TaskTrackAccept();
            this._taskAccept.y = 22;
            this._bodySprite.addChild(this._taskAccept);
            this._taskCurrent = new TaskTrackCurrent();
            this._taskCurrent.y = 22;
            this._bodySprite.addChild(this._taskCurrent);
            this.showCurrent();
            this._moveBtn.toolTipData = HTMLUtil.addColor(Language.getString(20713), "#FFFFFF");
            this._shrinkBtn.toolTipData = HTMLUtil.addColor(Language.getString(20714), "#ffffff");
            return;
        }

        /* internal  */onAlphaBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            this._bgSprite.alpha = this._bgSprite.alpha + this._alphaChange * this._alpahStep;
            if (this._bgSprite.alpha >= 1.6) 
            {
                this._bgSprite.alpha = 1.6;
            }
            if (this._bgSprite.alpha <= 0 || this._bgSprite.alpha >= 1.6) 
            {
                this._alphaChange = this._alphaChange * -1;
            }
            return;
        }

        /* internal  */onOpenNameBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._openBtn.isClose) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.TrackInfoShowOrHide, false));
            }
            else 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.TrackInfoShowOrHide, true));
            }
            return;
        }

        /* internal  */onShrinkBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._heightChange != 1) 
            {
                if (this._height != TaskTrackPanel.minHieght) 
                {
                    if (this._height != TaskTrackPanel.midHeight) 
                    {
                        if (this._height == TaskTrackPanel.maxHeight) 
                        {
                            this._height = TaskTrackPanel.midHeight;
                        }
                    }
                    else 
                    {
                        this._height = TaskTrackPanel.minHieght;
                    }
                }
                else 
                {
                    this._height = TaskTrackPanel.midHeight;
                    this._heightChange = 1;
                }
            }
            else if (this._height != TaskTrackPanel.minHieght) 
            {
                if (this._height != TaskTrackPanel.midHeight) 
                {
                    if (this._height == TaskTrackPanel.maxHeight) 
                    {
                        this._height = TaskTrackPanel.midHeight;
                        this._heightChange = -1;
                    }
                }
                else 
                {
                    this._height = TaskTrackPanel.maxHeight;
                }
            }
            else 
            {
                this._height = TaskTrackPanel.midHeight;
            }
            this.changHeight();
            return;
        }

        /* internal  */autoChangeHeight(): void
        {
            if (this._tabBar.selectedIndex != 0) 
            {
                if (this._taskAccept.bodyHeight > this._height && this._height == TaskTrackPanel.midHeight) 
                {
                    this._height = TaskTrackPanel.maxHeight;
                    this.changHeight();
                }
                else if (this._taskAccept.bodyHeight < TaskTrackPanel.midHeight && this._height == TaskTrackPanel.maxHeight) 
                {
                    this._height = TaskTrackPanel.midHeight;
                    this.changHeight();
                }
            }
            else if (this._taskCurrent.bodyHeight > this._height && this._height == TaskTrackPanel.midHeight) 
            {
                this._height = TaskTrackPanel.maxHeight;
                this.changHeight();
            }
            else if (this._taskCurrent.bodyHeight < TaskTrackPanel.midHeight && this._height == TaskTrackPanel.maxHeight) 
            {
                this._height = TaskTrackPanel.midHeight;
                this.changHeight();
            }
            return;
        }

        public showCurrent(arg1: Boolean=true): void
        {
            this._tabBar.selectedIndex = 0;
            if (this._taskAccept.parent) 
            {
                this._bodySprite.removeChild(this._taskAccept);
            }
            if (this._height != 0) 
            {
                if (!this._taskCurrent.parent) 
                {
                    this._bodySprite.addChild(this._taskCurrent);
                }
            }
            if (arg1) 
            {
                this.autoChangeHeight();
            }
            return;
        }

        public showAccept(arg1: Boolean=true): void
        {
            this._tabBar.selectedIndex = 1;
            if (this._taskCurrent.parent) 
            {
                this._bodySprite.removeChild(this._taskCurrent);
            }
            if (this._height != 0) 
            {
                if (!this._taskAccept.parent) 
                {
                    this._bodySprite.addChild(this._taskAccept);
                }
            }
            if (arg1) 
            {
                this.autoChangeHeight();
            }
            return;
        }

        /* internal  */changHeight(): void
        {
            var loc1=0;
            loc1 = this._height - this._bgImg.height;
            this._bgImg.height = this._bgImg.height + loc1;
            this._bottomLine.y = this._bottomLine.y + loc1;
            this._taskCurrent.updateHeight(loc1);
            this._taskAccept.updateHeight(loc1);
            if (this._height != 0) 
            {
                if (this._tabBar.selectedIndex != 0) 
                {
                    this.showAccept(false);
                }
                else 
                {
                    this.showCurrent(false);
                }
            }
            else 
            {
                if (this._taskCurrent.parent) 
                {
                    this._bodySprite.removeChild(this._taskCurrent);
                }
                if (this._taskAccept.parent) 
                {
                    this._bodySprite.removeChild(this._taskAccept);
                }
            }
            return;
        }

        public myTaskListInit(arg1: Array<any>): void
        {
            this._taskCurrent.initTask(arg1);
            if (arg1.length != 0) 
            {
                this.showCurrent();
            }
            else 
            {
                this.showAccept();
            }
            return;
        }

        public taskAdd(arg1: TaskInfo): void
        {
            this._taskCurrent.addTask(arg1);
            this._taskCurrent.sort();
            var loc1=Cache.instance.task.canGetTask.length;
            if (loc1 == 0) 
            {
                this.showCurrent();
            }
            this.autoChangeHeight();
            return;
        }

        public taskDel(arg1: int): void
        {
            var loc1=Cache.instance.task.getTaskByGroup(ETaskGroup._ETaskGroupMain).length;
            this._taskCurrent.delTask(arg1);
            if (loc1 == 0) 
            {
                this.showAccept();
                this.guideHide();
            }
            this.autoChangeHeight();
            return;
        }

        public taskUpdate(arg1: Array<any>): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=arg1;
            for(loc1 of loc3) 
            {
                this._taskCurrent.updateTask(loc1);
            }
            this._taskCurrent.sort();
            this.autoChangeHeight();
            return;
        }

        public acceptTaskRefresh(arg1: Array<any>): void
        {
            this._taskAccept.initTask(arg1);
            if (arg1.length == 0) 
            {
                this.showCurrent();
            }
            this.autoChangeHeight();
            return;
        }

        public acceptTaskAdd(arg1: Array<any>): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=0;
            var loc4=arg1;
            for(loc1 of loc4) 
            {
                this._taskAccept.addTask(loc1);
            }
            this._taskAccept.sort();
            loc2 = Cache.instance.task.myTaskList.length;
            if (loc2 == 0) 
            {
                this.showAccept();
            }
            this.autoChangeHeight();
            return;
        }

        public acceptTaskDel(arg1: int): void
        {
            this._taskAccept.delTask(arg1);
            if (Cache.instance.task.canGetTask.length == 0) 
            {
                this.showCurrent();
            }
            this.autoChangeHeight();
            return;
        }

        public stageResize(): void
        {
            x = Global.stage.stageWidth - this.width;
            y = 180 + 20 * Global.stage.stageHeight / 1000;
            return;
        }

        public guideTips(arg1: TaskInfo): void
        {
            this._taskCurrent.guideTips(arg1);
            return;
        }

        public guideHide(): void
        {
            this._taskCurrent.guideHide();
            return;
        }

        public guideDailyAct(arg1: TaskInfo): void
        {
            if (!TaskCache.showTrack) 
            {
                this.showTrack();
            }
            this.showCurrent(false);
            this.guideTips(arg1);
            return;
        }

        public onStageResizeF8(): void
        {
            this._height = TaskTrackPanel.midHeight;
            this.changHeight();
            return;
        }

        public /* override */ show(arg1: int=0, arg2: int=0): void
        {
            super.show();
            this.stageResize();
            return;
        }

        public showTrack(): void
        {
            if (this._hideTween && this._hideTween.active) 
            {
                this._hideTween.kill();
            }
            x = SceneRange.display.width - this.width;
            y = 180 + 20 * SceneRange.display.height / 1000;
            this._showTween = TweenMax.to(this._bodySprite, 0.2, {"x": 0, "ease": Quint.easeIn});
            this._hideBtn.show = true;
            TaskCache.showTrack = true;
            if (!this._bodySprite.parent) 
            {
                addChild(this._bodySprite);
            }
            return;
        }

        public /* override */ get width(): number
        {
            return 232;
        }

        /* internal  */onCycleBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            GameManager.instance.popupWindow(ModuleType.Tasks);
            return;
        }

        /* internal  */onTabBarChangeHandler(arg1: MuiEvent): void
        {
            var loc1=this._tabBar.selectedIndex;
            if (loc1 != 0) 
            {
                this.showAccept();
            }
            else 
            {
                this.showCurrent();
            }
            return;
        }

        /* internal  */onHideBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (TaskCache.showTrack) 
            {
                this.hideTrack();
            }
            else 
            {
                this.showTrack();
            }
            this.dispatchEvent(new DataEvent(EventName.TrackClick, TaskCache.showTrack));
            Dispatcher.dispatchEvent(new DataEvent(EventName.TrackClick, TaskCache.showTrack));
            return;
        }

        /* internal  */onMoveBtnMouseHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            this._dragRect.x = 0;
            this._dragRect.y = 0;
            this._dragRect.width = SceneRange.display.width - 30;
            this._dragRect.height = SceneRange.display.height - 30;
            startDrag(false, this._dragRect);
            Global.stage.addEventListener(flash.events.MouseEvent.MOUSE_UP, this.onStageMouseUpHandler);
            return;
        }

        /* internal  */onStageMouseUpHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            stopDrag();
            Global.stage.removeEventListener(flash.events.MouseEvent.MOUSE_UP, this.onStageMouseUpHandler);
            return;
        }

        public guideVipFly(arg1: TaskInfo): void
        {
            if (!TaskCache.showTrack) 
            {
                this.showTrack();
            }
            this.showCurrent(false);
            this.guideTips(arg1);
            return;
        }

        public hideTrack(): void
        {
            if (this._showTween && this._showTween.active) 
            {
                this._showTween.kill();
            }
            x = SceneRange.display.width - this.width;
            y = 180 + 20 * SceneRange.display.height / 1000;
            this._hideTween = TweenMax.to(this._bodySprite, 0.2, {"x": this.width, "ease": Quint.easeOut, "onComplete": this.onHideEnd});
            this._hideBtn.show = false;
            TaskCache.showTrack = false;
            return;
        }

        /* internal  */onHideEnd(): void
        {
            if (this._bodySprite.parent) 
            {
                removeChild(this._bodySprite);
            }
            return;
        }

        public /* const */taskTabData: Array<any>=[{"label": Language.getString(20710), "name":"1"}, {"label": Language.getString(20711), "name":"2"}];

        /* internal */ static /* const */maxHeight: int=230;

        /* internal */ static /* const */midHeight: int=145;

        /* internal */ static /* const */minHieght: int=0;

        /* internal */ /* var */_bodySprite: Object /* flash.display.Sprite */;

        /* internal */ /* var */_bgSprite: Object /* flash.display.Sprite */;

        /* internal */ /* var */_hideBtn: ShowBtn;

        /* internal */ /* var */_moveBtn: GButton;

        /* internal */ /* var */_tabBar: GTabBar;

        /* internal */ /* var */_openBtn: OpenNameBtn;

        /* internal */ /* var */_shrinkBtn: GButton;

        /* internal */ /* var */_upLine: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_bottomLine: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_bgImg: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_dragRect: Object /* flash.geom.Rectangle */;

        /* internal */ /* var */_cycleBtn: ButtonMini;

        /* internal */ /* var */_taskAccept: TaskTrack;

        /* internal */ /* var */_taskCurrent: TaskTrack;

        /* internal */ /* var */_alphaChange: int=-1;

        /* internal */ /* var */_alpahStep: number=0.5;

        /* internal */ /* var */_heightChange: int=1;

        /* internal */ /* var */_height: int=145;

        /* internal */ /* var */_showTween: TweenMax;

        /* internal */ /* var */_hideTween: TweenMax;

        /* internal */ /* var */_alphaBtn: GButton;
    }
